import pygame
from pygame.locals import *

from lib.Loader import Loader

from lib.Puzzle import PuzzleFactory    

# LoadPart se encarga de cargar las imagenes atributo del diagrama y 
# el texto.
class LoadPart(object):
    def __init__(self,lecture_name):
        
        self.loader = Loader(lecture_name)
        self.pieces = self.loader.list_atributes()
        self.back   = self.loader.load_image("logo.png").convert()
        #self.image  = self.loader.load_motive(filename).convert_alpha()
        self.fnt    = self.loader.load_font("FreeSans.ttf",30)
                
        self.pf = PuzzleFactory(self.image,mask_image,bevel_image,shadow_image)
        
        self.px = 0
        self.py = 0
    
        self.bar = pygame.Rect(0,0,400,20)        
        self.box = pygame.Rect(0,0,404,24)
        
        self.bar.center = (400,470)
        self.box.center = (400,470)

        self.next_part = 0

    def event(self,event):
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.next_part = -1

    def get_next(self):
        self.pieces.append(self.pf._make_piece(self.px,self.py))
        self.px+=1
        if self.px==10:
            self.px = 0
            self.py += 1
            if self.py == 8:
                self.next_part = 1

    def update(self,screen):
        self.get_next()
        #screen.blit(self.back,(0,0))

        txt = self.fnt.render("Cutting pieces %d/80" % len(self.pieces), True, (255,255,0))
        txtr = txt.get_rect()
        txtr.center = (400,430)

        #screen.blit(txt,txtr.topleft)        

        pygame.draw.rect(screen, (255,255,255), self.box, 1)
        self.bar.w = int(round((len(self.pieces)/80.0)*400.0))
        screen.fill((255,255,255),self.bar)
        return self.next_part

